#!/bin/bash

LOG_PATH="logs/config_vrf.log"

echo "============================= add_vlan_vrf start =============================" >> $LOG_PATH 2>&1

if (( $# < 3 )); then
  echo "failed"
  echo "input params is short. need input vlan_id vlan_addr default_gw" >> $LOG_PATH 2>&1
  echo "need input vlan_id vlan_addr default_gw"
  echo "eg $0 3 10.10.10.13/24 10.10.10.1"
  exit;
fi
vlan_id=$1
vrf_id=$1
vlan_addr=$2
default_gw=$3
net_card_name="ens33"

# 开启网卡
sudo ip link set $net_card_name up >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  echo "sudo ip link set $net_card_name up" >> $LOG_PATH 2>&1
  exit 1
fi

# 添加VLAN接口
sudo ip link add link $net_card_name name vlan$vlan_id type vlan id $vlan_id >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  echo "sudo ip link add link $net_card_name name vlan$vlan_id type vlan id $vlan_id" >> $LOG_PATH 2>&1
  exit 1
fi

# 设置VLAN接口的IP地址
sudo ip addr add $vlan_addr dev vlan$vlan_id >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  # 错误处理，回收VLAN接口
  echo "sudo ip addr add $vlan_addr dev vlan$vlan_id" >> $LOG_PATH 2>&1
  sudo ip link delete vlan$vrf_id type vlan
  exit 1
fi

# 开启VLAN接口
sudo ip link set vlan$vlan_id up >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  # 错误处理，回收VLAN接口
  echo "sudo ip link set vlan$vlan_id up" >> $LOG_PATH 2>&1
  sudo ip link delete vlan$vrf_id type vlan
  exit 1
fi

# 创建VRF实例
sudo ip link add vrf$vrf_id type vrf table $vrf_id >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  # 错误处理，回收VLAN接口
  echo "sudo ip link add vrf$vrf_id type vrf table $vrf_id" >> $LOG_PATH 2>&1
  sudo ip link set vlan$vrf_id down
  sudo ip link delete vlan$vrf_id type vlan
  exit 1
fi

# 开启VRF实例
sudo ip link set vrf$vrf_id up >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  # 错误处理，回收VLAN接口和VRF实例
  echo "sudo ip link set vrf$vrf_id up" >> $LOG_PATH 2>&1
  sudo ip link set vlan$vrf_id down
  sudo ip link delete vlan$vrf_id type vlan
  sudo ip link delete vrf$vrf_id type vrf
  exit 1
fi

# 将VLAN接口绑定到VRF实例
sudo ip link set vlan$vlan_id master vrf$vrf_id >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  # 错误处理，回收VLAN接口和VRF实例
  echo "sudo ip link set vlan$vlan_id master vrf$vrf_id" >> $LOG_PATH 2>&1
  sudo ip link set vlan$vrf_id down
  sudo ip link delete vlan$vrf_id type vlan
  sudo ip link set vrf$vrf_id down
  sudo ip link delete vrf$vrf_id type vrf
  exit 1
fi

# 设置默认路由
sudo ip route add default via $default_gw dev vlan$vlan_id vrf vrf$vrf_id >> $LOG_PATH 2>&1
if [ $? -ne 0 ]; then
  echo "failed"
  # 错误处理，解除绑定并回收VLAN接口和VRF实例
  echo "sudo ip route add default via $default_gw dev vlan$vlan_id[$vlan_addr] vrf vrf$vrf_id" >> $LOG_PATH 2>&1
  sudo ip link set vlan$vrf_id nomaster
  sudo ip link set vlan$vrf_id down
  sudo ip link delete vlan$vrf_id type vlan
  sudo ip link set vrf$vrf_id down
  sudo ip link delete vrf$vrf_id type vrf
  exit 1
fi

echo "add vlan success:" >> $LOG_PATH 2>&1
echo "vlan_id[$1] vlan_addr[$2] dev_name[$3] default_gw[$4]" >> $LOG_PATH 2>&1
echo "success"
exit 0